<template>
  <div>
    <!--1.视图-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="请假人" prop="leaveName">
        <el-input
          v-model="queryParams.leaveName"
          placeholder="请输入请假人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请假标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入请假标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker
          v-model="timeValue"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="送审人" prop="proposer">
        <el-input
          v-model="queryParams.proposer"
          placeholder="请输入请假送审人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['note:applys:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          v-hasPermi="['note:applys:edit']"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['note:applys:remove']"
        >删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="请假编号" align="center" key="leaveId" prop="leaveId" />
      <el-table-column label="请假名称" align="center" key="leaveName" prop="leaveName" />
      <el-table-column label="请假标题" align="center" key="title" prop="title"/>
      <el-table-column label="请假原因" align="center" key="reason" prop="reason" />
      <el-table-column label="请假送审人" align="center" key="proposer" prop="proposer"/>
      <el-table-column label="请假附件图片" align="center" key="leaveImg" prop="leaveImg">
        <template v-slot="s">
          <img :src="s.row.leaveImg" style="width: 100px;height: 100px;">
          </img>
        </template>
      </el-table-column>
      <el-table-column label="请假状态" align="center" key="status" prop="status">
        <template v-slot="s">
          <el-button  size="mini" type="info" v-if="s.row.status==0">未申请</el-button>
          <el-button  size="mini" type="success" v-if="s.row.status==1">申请中</el-button>
          <el-button  size="mini" type="info" v-if="s.row.status==2">申请完毕</el-button>
          <el-button  size="mini" type="danger" v-if="s.row.status==3">未驳回</el-button>
          <el-button  size="mini" type="danger" v-if="s.row.status==4">已驳回</el-button>
        </template>
      </el-table-column>
      <el-table-column label="请假备注" align="center" key="remark" prop="remark"/>
      <el-table-column label="实例Id" align="center" key="procId" prop="procId"/>
      <el-table-column label="开始时间" align="center" key="startTime" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" key="endTime" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['note:applys:edit']"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.status==0 || scope.row.status==4"
            @click="apply(scope.row)"
          >送审</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['note:applys:remove']"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="form"  label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="请假人" prop="leaveName">
                <el-input v-model="loginName" disabled placeholder="请输入请假人" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="请假标题" prop="title">
                <el-input v-model="form.title" placeholder="请输入请假标题" />
              </el-form-item>
            </el-col>
          </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                v-model="timeValue"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                :picker-options="pickerOptions"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="请假原因" prop="reason">
              <el-input v-model="form.reason" placeholder="请输入请假原因" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请假备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入请假备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="请假天数" prop="proposer">
              <el-input v-model="form.days" placeholder="请输入请假天数" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="送审人" prop="proposer">
              <el-input v-model="form.proposer" placeholder="请输入请假送审人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="附件图片" prop="leaveImg">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:9201/upload/uploadFile"
                :show-file-list="false"
                :on-success="handleAvatarSuccess">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {
  selectList,
  updateLeave,
  addLeave,
  getLeave,
  delLeave,
  goApply,
} from "@/api/note/applys";
import user from "@/store/modules/user";
export default {
  name: "index",
  data() {
    return {
      //2.属性定义
      loginName: user.state.name,
      dialogFormVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      imageUrl:'',
      ids: [],
      timeValue: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dataList: [],
      // 默认字典类型
      defaultLeaveType: "",
      // 弹出层标题
      title: "",
      // 类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        LeaveName: undefined,
        proposer: undefined,
        title: undefined
      },
      pickerOptions:{
        disabledDate(time){
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      // 表单参数
      form: {},
      flag:0,
    }
  }, methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.leaveImg=res;
    },
    apply(row){
      goApply(row.procId,row.proposer,row.days,this.flag).then(r=>{
        this.getList();
      })
    },
    //3.方法
    submitForm(){
      if (this.form.leaveId != null) {
        this.form.leaveName=this.loginName;
        this.form.startTime=this.timeValue[0];
        this.form.endTime=this.timeValue[1];
        updateLeave(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.dialogFormVisible = false;
          this.getList();
        });
      } else {
        this.form.leaveName=this.loginName;
        this.form.startTime=this.timeValue[0];
        this.form.endTime=this.timeValue[1];
        addLeave(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.dialogFormVisible = false;
          this.getList();
        });
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row){
      this.form.leaveName=this.loginName;
      this.imageUrl=row.leaveImg;
      const leaveId = row.leaveId || this.ids;
      this.timeValue=[];
      getLeave(leaveId).then(response => {
        this.form = response.data;
        this.title = "修改";
        this.timeValue.push(this.form.startTime);
        this.timeValue.push(this.form.endTime);
        this.dialogFormVisible =  true;
      });
    },
    handleAdd(){
      this.form.leaveName=this.loginName;
      this.form.leaveImg=this.imageUrl;
      console.log(this.form.leaveName);
      this.form={};
      this.title = "添加";
      this.dialogFormVisible = true;
    },
    handleDelete(row){
      const leaveIds = row.leaveId || this.ids;
      this.$modal.confirm('是否确认删除详情编号为"' + leaveIds + '"的数据项？').then(function() {
        return delLeave(leaveIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    getList(){
      this.loading=true;
      selectList(this.queryParams).then(r=>{
        this.dataList = r.rows;
        this.total=r.total;
        this.loading=false;
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leaveId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.LeaveName = this.defaultLeaveType;
      this.handleQuery();
    },
  }, created() {
    //4.钩子函数
    this.getList();
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
